package com.alameda.common.config.zookeeper;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * zookeeper配置类
 *
 * @author Lv
 * @date 2024/11/7
 */
@Configuration
public class ZookeeperConfig {

    /**
     * zookeeper地址
     */
    @Value("${zookeeper.connect.address}")
    private String connectString;

    /**
     * 创建并配置CuratorFramework实例
     * <p>
     * CuratorFramework是Apache Curator提供的一个高度封装的ZooKeeper客户端框架
     * 它简化了与ZooKeeper的交互过程，并且内置了重试机制、连接状态管理等功能
     *
     * @return CuratorFramework 实例，用于与ZooKeeper进行交互
     */
    @Bean
    public CuratorFramework curatorFramework() {
        // 实例化CuratorFramework客户端，使用指数退避重试策略
        CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, new ExponentialBackoffRetry(1000, 3));

        // 启动CuratorFramework客户端
        client.start();

        // 返回配置好的CuratorFramework实例
        return client;
    }
}